<template>
  <Breadcrumb />
  <el-card class="box-card">
    <!-- 头部搜索 -->
    <el-row class="row-bg">
      <el-col :span="8">
        <el-input
          v-model="GoodInfo.query"
          placeholder="请输入内容"
          class="input-with-select"
          clearable
          @change="getDataList"
        >
          <template #append>
            <el-button :icon="Search" @click="searchChange" />
          </template>
        </el-input>

      </el-col>
          <el-button class="btn" type="primary" @click="addUser">添加用户</el-button>
      
  </el-row>
  <!-- 主体内容 -->
    <el-table :data="tableData" border style="width: 100%">
      <el-table-column type="index" prop="address" label="" width="50" />
      <el-table-column prop="goods_name" label="商品名称" />
      <el-table-column prop="goods_price" label="商品价格(元)" width="100" />
      <el-table-column prop="goods_weight" label="商品重量" width="100" />
      <el-table-column prop="upd_time" label="创建时间" width="200" />
      <el-table-column label="操作" width="170">
        <template #default="scope">
          <el-button type="primary" :icon="Edit" size="small">编辑</el-button>
          <el-button type="danger" :icon="Delete" size="small">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页器 -->
    <el-pagination
      v-model:currentPage="GoodInfo.pagenum"
      v-model:page-size="GoodInfo.pagesize"
      :page-sizes="[5, 20, 30, 40]"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />
  </el-card>
</template>

<script setup>
import { ref } from 'vue'
import { Search } from '@element-plus/icons-vue'
import { Edit, Delete } from '@element-plus/icons-vue'
import Breadcrumb from '@/components/Breadcrumb.vue'
import { getGoods } from '@/api/goods'
import { useRouter } from 'vue-router'

const router = useRouter()
const GoodInfo = ref({
  query: '',
  pagenum: 1,
  pagesize: 5
})
const tableData = ref([])
const total = ref(0)
const getDataList = async () => {
  const res = await getGoods(GoodInfo.value)
  tableData.value = res.data.data.goods
  total.value = res.data.data.total
  console.log(total.value)
  console.log(res)
}
getDataList()

// 分页器点击事件
const handleSizeChange = (size) => {
  GoodInfo.value.pagesize = size
  getDataList()
}
const handleCurrentChange = (current) => {
  GoodInfo.value.pagenum = current
  getDataList()
}

// 搜索点击事件
const searchChange = () => {
  getDataList()
}
// 添加用户事件
const addUser = () => {
  router.replace('/goods/add')
}

</script>

<style scoped lang="less">
.el-card{
  .el-table{
    font-size: 12px;
    margin-top: 15px;
  }
  margin-top: 15px;
  .btn{
    margin-left: 15px;
  }
}
</style>
